
import gd from '@/common/json/gd.json';
import gddata from '@/common/json/data.json';
import styleList from '@/common/js/styleList.js';
import { getXAxis, getYAxis } from './common';

export const handleRadar = function () {
  let chartData = {
    series: [{
      type: 'radar',
      areaStyle: {},
      data: [
        {
          name: '成交量1',
          data: [90, 110, 165, 195, 187, 172],
        },
        {
          name: '成交量2',
          data: [190, 210, 105, 35, 27, 102],
        },
      ].map(v => ({
        name: v.name,
        value: v.data,
        lineStyle: {
          color: v.name == '成交量1' ? '#FFB951' : '#ff0000',
        },
        itemStyle: {
          color: v.name == '成交量1' ? '#FFB951' : '#ff0000',
        },
        areaStyle: {
          color: v.name == '成交量1' ? '#FFB951' : '#ff0000',
          opacity: 0.3,
        },
      })),
    }]
  }
  let eopts = {
    legend: {
      bottom: 0,
      itemWidth: 8,
      textStyle: {
        color: '#fff'
      }
    },
    radar: {
      center: ['50%', '47%'],
      splitArea: {
        areaStyle: {
          color: 'rgba(0, 0, 0, 0)',
        },
      },
      axisLine: {
        lineStyle: {
          color: '#4DB6FF',
        },
      },
      splitLine: {
        lineStyle: {
          color: '#4DB6FF',
        },
      },
      shape: 'circle',
      name: {
        format: 'radar2Format',
        fontSize: 10,
      },
      indicator: [
        { targetName: '维度1', targetValue: 25, unit: '亿' },
        { targetName: '维度2', targetValue: 15, unit: '亿' },
        { targetName: '维度3', targetValue: 10, unit: '亿' },
        { targetName: '维度4', targetValue: 15, unit: '亿' },
        { targetName: '维度5', targetValue: 20, unit: '亿' },
        { targetName: '维度6', targetValue: 15, unit: '亿' },
      ].map(v =>
      ({
        name: `${v.targetName}(${v.targetValue}${v.unit})`,
        max: 300,
        min: -10,
        color: "#fff"
      })
      ),
      triggerEvent: true,
      radius: '50%',
    },
  }
  return {
    chartData, eopts, type: 'radar'
  }
}

export const handleMap = function () {
  let chartData = {
    series: gd.features,
  }
  let eopts = {
    fontColor: '#fff',
    padding: [20, 20, 20, 20],
    extra: {
      map: {
        border: true,
        borderWidth: 1,
        borderColor: '#40AEE1',
        fillOpacity: 0.6,
        activeBorderColor: '#40AEE1',
        activeFillColor: '#FACC14',
        activeFillOpacity: 1,
      },
    },
    // series: JSON.stringify({
    //   data: gddata.map(v => ({
    //     name: v.name,
    //     value: v.population,
    //     targetId: v.adcode,
    //     codeFen: v.adcode,

    //     tooltip: {
    //       formatter: `${v.name}:${v.population}`
    //     },
    //     itemStyle: {
    //       borderColor: '#40AEE1',
    //       borderWidth: 1.5,
    //     },
    //     emphasis: {
    //       label: {
    //         color: '#E84130'
    //       }
    //     },
    //     select: {
    //       label: {
    //         color: '#fff'
    //       }
    //     }
    //   }))
    // })
  }
  return {
    chartData, eopts, type: 'map'
  }
}

export const handleZfBar = function () {
  const itemData = [
    {
      data: [
        {
          targetName: '转变率',
          x: '数据1',
          score: 10,
          scoreUnit: '万'
        },
        {
          targetName: '转变率',
          x: '数据2',
          score: 20,
          scoreUnit: '万'
        },
        {
          targetName: '转变率',
          x: '数据3',
          score: -15,
          scoreUnit: '万'
        },
        {
          targetName: '转变率',
          x: '数据4',
          score: 30,
          scoreUnit: '万'
        },
        {
          targetName: '转变率',
          x: '数据5',
          score: -25,
          scoreUnit: '万'
        },
        {
          targetName: '转变率',
          x: '数据6',
          score: -10,
          scoreUnit: '万'
        },
        {
          targetName: '转变率',
          x: '数据7',
          score: 20,
          scoreUnit: '万'
        },
        {
          targetName: '转变率',
          x: '数据8',
          score: -15,
          scoreUnit: '万'
        },
        {
          targetName: '转变率',
          x: '数据9',
          score: 30,
          scoreUnit: '万'
        },
        {
          targetName: '转变率',
          x: '数据10',
          score: -25,
          scoreUnit: '万'
        },
        {
          targetName: '转变率',
          x: '数据1',
          score: 10,
          scoreUnit: '万'
        },
        {
          targetName: '转变率',
          x: '数据2',
          score: 20,
          scoreUnit: '万'
        },
        {
          targetName: '转变率',
          x: '数据3',
          score: -15,
          scoreUnit: '万'
        },
        {
          targetName: '转变率',
          x: '数据4',
          score: 30,
          scoreUnit: '万'
        },
        {
          targetName: '转变率',
          x: '数据5',
          score: -25,
          scoreUnit: '万'
        },
        {
          targetName: '转变率',
          x: '数据6',
          score: 10,
          scoreUnit: '万'
        },
        {
          targetName: '转变率',
          x: '数据7',
          score: 30,
          scoreUnit: '万'
        },
        {
          targetName: '转变率',
          x: '数据8',
          score: -15,
          scoreUnit: '万'
        },
        {
          targetName: '转变率',
          x: '数据9',
          score: 20,
          scoreUnit: '万'
        },
        {
          targetName: '转变率',
          x: '数据10',
          score: -25,
          scoreUnit: '万'
        },
      ]
    }
  ]
  let seriesq = {
    type: 'bar',
    ...styleList[11],
    barMinHeight: 5,
    name: itemData[0].data[0].targetName + '正',
    itemStyle: {
      color: "#1266FF"
    },
    data: []
  }
  let seriesh = {
    type: 'bar',
    ...styleList[11],
    barMinHeight: 5,
    name: itemData[0].data[0].targetName + '负',
    itemStyle: {
      color: "#FFB07B"
    },
    data: []
  }
  let categories = []
  itemData[0].data.forEach(e => {
    if (e.score !== undefined) {
      categories.push(e.x);
    }
    if (e.score !== undefined) {
      if (e.score >= 0 || isNaN(e.score)) {
        if (e.score > 400) {
          e.score = 400
          xAxis.max = 450
        }
        seriesq.data.push({
          value: isNaN(e.score) ? 0 : e.score,
          reValue: e.score,
          u: e.scoreUnit
        })
        seriesh.data.push(null)
      } else {
        if (e.score < -400) {
          e.score = -400
          xAxis.min = -450
        }
        seriesq.data.push(null)
        seriesh.data.push({ value: e.score, u: e.scoreUnit })
      }
    }
  })

  let chartData = {
    series: [seriesq, seriesh],
    categories
  }
  let eopts = {
    tooltip: styleList['tooltip1'],
    grid: {
      left: '3%',
      right: '8%',
      bottom: '3%',
      containLabel: true
    },
    xAxis: [{
      name: "单位:" + itemData[0].data[0].scoreUnit,
      nameTextStyle: {
        verticalAlign: "bottom",
        color: "#ffffff",
        padding: [0, 0, 60, -50],
        fontSize: 12,
      },
      position: 'top',
      type: 'value',
      axisLabel: {
        color: "#ffffff",
        fontSize: 11
      },
      splitLine: {
        lineStyle: {
          color: "#ffffff",
          opacity: 0.1
        }
      }
    }],
    yAxis: [
      {
        type: "category",
        axisTick: {
          show: false
        },
        axisLabel: {
          color: "#ffffff",
          interval: 0,
          fontSize: 11
        },
        data: categories,
      }
    ],
    legend: {
      data: chartData.series.map(e => e.name),
      textStyle: {
        fontSize: 12,
        color: "#24e8ff"
      },
      itemWidth: 16,
      itemHeight: 10,
      top: 0
    }
  }
  return {
    chartData, eopts, type: 'bar'
  }
}

export const handleBfBar = function () {
  const itemData = [
    {
      data: [
        {
          x: '1',
          needY: false,
          targetValue: 25,
          targetName: '预测值',
          unit: '万'
        },
        {
          x: '1',
          needY: true,
          targetValue: 30,
          targetName: '实际值',
          unit: '万'
        },
        {
          x: '2',
          needY: false,
          targetValue: 35,
          targetName: '预测值',
          unit: '万'
        },
        {
          x: '2',
          needY: true,
          targetValue: 45,
          targetName: '实际值',
          unit: '万'
        },
        {
          x: '3',
          needY: false,
          targetValue: 45,
          targetName: '预测值',
          unit: '万'
        },
        {
          x: '3',
          needY: true,
          targetValue: 40,
          targetName: '实际值',
          unit: '万'
        },
        {
          x: '4',
          needY: false,
          targetValue: 40,
          targetName: '预测值',
          unit: '万'
        },
        {
          x: '4',
          needY: true,
          targetValue: 35,
          targetName: '实际值',
          unit: '万'
        },
        {
          x: '5',
          needY: false,
          targetValue: 60,
          targetName: '预测值',
          unit: '万'
        },
        {
          x: '5',
          needY: true,
          targetValue: 55,
          targetName: '实际值',
          unit: '万'
        },
        {
          x: '6',
          needY: false,
          targetValue: 55,
          targetName: '预测值',
          unit: '万'
        },
        {
          x: '6',
          needY: true,
          targetValue: 55,
          targetName: '实际值',
          unit: '万'
        },
      ]
    }
  ]

  let series = []
  // if (itemData[0].data.length > 12) {    //x轴大于12 存在预测值
  // 柱状图结构   （保费）
  let bf = {      //保费
    ...styleList[3],
    name: '实际值',
    barMinHeight: 5,
    yAxisIndex: 0,
    data: [],
  }
  let yc = {      //预测
    ...styleList[3],
    name: '预测值',
    barMinHeight: 5,
    yAxisIndex: 0,
    data: [],
    itemStyle: {
      color: '#1473a7',
      borderRadius: [5, 5, 0, 0]
    },
  }
  let mbx = {
    ...styleList[7],
    name: '目标线',
    yAxisIndex: 0,
    data: [
      25, 30, 40, 35, 45, 40, 30, 30, 40, 35, 45, 50
    ].map(e => ({ value: e, u: '万' }))
  }
  itemData.forEach(ele => {
    ele.data.forEach(e => {
      if (e.targetName == '预测值') {
        yc.data.push({ value: e.targetValue, u: '万' })
        bf.data.push(null)
      } else {    //保费
        yc.data.push(null)
        bf.data.push({
          value: e.targetValue,
          u: '万',
        })
      }
    })
  })
  // 存在保费 或者 预测值   push到series
  Math.max(...(yc.data)) !== 0 && series.push(yc)
  Math.max(...(bf.data)) !== 0 && series.push(bf)
  //   push目标线到series
  series.push(mbx)
  // } else {
  //   series = itemData.map(ele => ({
  //     ...styleList[ele.style],
  //     barMinHeight: 5,
  //     barMaxWidth: 10,
  //     name: ele.data[0].targetName,
  //     yAxisIndex: ele.data[0].unit == '%' ? 1 : 0,
  //     data: ele.data.map(e => ({
  //       value: e.targetValue,
  //       u: e.unit,
  //     }))
  //   }))
  // }
  let chartData = { series }
  let eopts = {
    tooltip: {
      ...styleList['tooltip1'],
      axisPointer: {
        type: 'line'
      }
    },
    grid: {
      left: '3%',
      right: '8%',
      bottom: '12%',
      containLabel: true,
    },
    legend: {
      itemWidth: 16,
      itemHeight: 10,
      bottom: 10,
      textStyle: {
        fontSize: 11,
        color: '#fff',
      },
    },
    xAxis: getXAxis(itemData),

    yAxis: getYAxis(itemData)
  }
  return {
    chartData, eopts, type: 'bar'
  }
}

export const handlePie = function () {
  const itemData = [
    {
      data: [
        {
          x: '数据1',
          needY: false,
          targetValue: 25,
          targetName: '预测值',
          unit: '万'
        },
        {
          x: '数据2',
          needY: true,
          targetValue: 30,
          targetName: '实际值',
          unit: '万'
        },
        {
          x: '数据3',
          needY: false,
          targetValue: 35,
          targetName: '预测值',
          unit: '万'
        },
        {
          x: '数据4',
          needY: true,
          targetValue: 45,
          targetName: '实际值',
          unit: '万'
        },
        {
          x: '数据5',
          needY: false,
          targetValue: 45,
          targetName: '预测值',
          unit: '万'
        },
        {
          x: '数据6',
          needY: true,
          targetValue: 40,
          targetName: '实际值',
          unit: '万'
        },
      ]
    }
  ]
  let chartData = {
    series: [{
      type: 'pie',
      ...styleList[2],
      areaStyle: {},
      data: itemData[0].data.map((v, vi) => ({
        name: v.x,
        value: v.targetValue,
        u: v.unit,
        itemStyle: styleList.itemStyle(vi)
      })),
    }]
  }
  let eopts = {
    tooltip: {
      ...styleList['tooltip2'],
    },
    color: ['#5d2ff4', '#fbac0e', '#f64545', '#4187f1', '#0bd4d1'],
    legend: {
      bottom: 0,
      itemWidth: 8,
      textStyle: {
        color: '#fff'
      }
    },
  }
  return {
    chartData, eopts, type: 'pie'
  }
}